//
//  XHShareViewController.m
//  umibus
//
//  Created by 冷山 on 16/6/22.
//  Copyright © 2016年 lexingsoft. All rights reserved.
//

#import "XHShareViewController.h"

#import "Services.h"

#import <UMSocial.h>

#import <UMengSocialCOM/UMSocial.h>
#import <SDWebImage/UIImageView+WebCache.h>

@interface XHShareViewController ()
@property (weak, nonatomic) IBOutlet UIButton *cancelButton;

@end

@implementation XHShareViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//微信
- (IBAction)weChatAction:(id)sender
{
    if ([self.shareType isEqualToString:@"app"])
    {
        [UMSocialData defaultData].extConfig.title = @"优米巴士";
        [UMSocialData defaultData].extConfig.wechatSessionData.url =[NSString stringWithFormat:@"%@/cuterats/sharelink/shareapp",[ServiceManager getHostAddress]];
        UIImageView * imageView = [[UIImageView alloc]init];
        [imageView sd_setImageWithURL:[NSURL URLWithString:@"http://o7s70rfog.bkt.clouddn.com/icon-umibus.png"] placeholderImage:[UIImage imageNamed:@"icon180"]];
        [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToWechatSession] content:@"用心服务，智慧创造" image:imageView.image location:nil urlResource:nil presentedController:self completion:^(UMSocialResponseEntity *shareResponse){
            if (shareResponse.responseCode == UMSResponseCodeSuccess) {
                NSLog(@"分享成功！");
                [self cancelAction:sender];
            }
        }];
    }
    else if ([self.shareType isEqualToString:@"coupons"])
    {
        [ServiceManager putShareCouponsWithSequenceNBR:self.sequenceNBR success:^(NSDictionary *data) {
            
        } failure:^(NSDictionary *data) {
            
        }];
        [UMSocialData defaultData].extConfig.title = @"赠送优惠券";
        [UMSocialData defaultData].extConfig.wxMessageType = UMSocialWXMessageTypeWeb;
        [UMSocialData defaultData].extConfig.wechatSessionData.url = [NSString stringWithFormat:@"%@/cuterats/sharelink?id=%@&random=%@",[ServiceManager getHostAddress],self.sequenceNBR,[self getTimeNow]];
        UIImageView * imageView = [[UIImageView alloc]init];
        [imageView sd_setImageWithURL:[NSURL URLWithString:@"http://o7s70rfog.bkt.clouddn.com/icon-umibus.png"] placeholderImage:[UIImage imageNamed:@"icon180"]];
        [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToWechatSession] content:@"优米巴士免费送流量话费了，领到就是赚到！" image:imageView.image location:nil urlResource:nil presentedController:self completion:^(UMSocialResponseEntity *shareResponse){
            if (shareResponse.responseCode == UMSResponseCodeSuccess) {
                NSLog(@"分享成功！");
                [self cancelAction:sender];
            }
        }];
    }
}
//朋友圈
- (IBAction)friendAction:(id)sender
{
    if ([self.shareType isEqualToString:@"app"])
    {
        [UMSocialData defaultData].extConfig.title = @"优米巴士-用心服务，智慧创造";
        [UMSocialData defaultData].extConfig.wechatTimelineData.url = [NSString stringWithFormat:@"%@/cuterats/sharelink/shareapp",[ServiceManager getHostAddress]];
        UIImageView * imageView = [[UIImageView alloc]init];
        [imageView sd_setImageWithURL:[NSURL URLWithString:@"http://o7s70rfog.bkt.clouddn.com/icon-umibus.png"] placeholderImage:[UIImage imageNamed:@"icon180"]];
        [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToWechatTimeline] content:nil image:imageView.image
                                                           location:nil urlResource:nil presentedController:self completion:^(UMSocialResponseEntity *shareResponse){
            if (shareResponse.responseCode == UMSResponseCodeSuccess) {
                NSLog(@"分享成功！");
                [self cancelAction:sender];
            }
        }];
    }
    else if ([self.shareType isEqualToString:@"coupons"])
    {
        [ServiceManager putShareCouponsWithSequenceNBR:self.sequenceNBR success:^(NSDictionary *data) {
            
        } failure:^(NSDictionary *data) {
            
        }];
        [UMSocialData defaultData].extConfig.title = @"优米巴士-免费送流量话费了，领到就是赚到！";
        [UMSocialData defaultData].extConfig.wechatTimelineData.url = [NSString stringWithFormat:@"%@/cuterats/sharelink?id=%@&random=%@",[ServiceManager getHostAddress],self.sequenceNBR,[self getTimeNow]];
        UIImageView * imageView = [[UIImageView alloc]init];
        [imageView sd_setImageWithURL:[NSURL URLWithString:@"http://o7s70rfog.bkt.clouddn.com/icon-umibus.png"] placeholderImage:[UIImage imageNamed:@"icon180"]];
        [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToWechatTimeline] content:nil image:imageView.image location:nil urlResource:nil presentedController:self completion:^(UMSocialResponseEntity *shareResponse){
            if (shareResponse.responseCode == UMSResponseCodeSuccess) {
                NSLog(@"分享成功！");
                [self cancelAction:sender];
            }
        }];
    }
}
//qq好友
- (IBAction)QQFriendAction:(id)sender
{
    if ([self.shareType isEqualToString:@"app"])
    {
        [UMSocialData defaultData].extConfig.title = @"优米巴士";
        UMSocialUrlResource *urlResource = [[UMSocialUrlResource alloc] initWithSnsResourceType:UMSocialUrlResourceTypeImage url:
                                            @"http://o7s70rfog.bkt.clouddn.com/icon-umibus.png"];
        [UMSocialData defaultData].extConfig.qqData.url = [NSString stringWithFormat:@"%@/cuterats/sharelink/shareapp",[ServiceManager getHostAddress]];
        [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToQQ] content:@"优米巴士-用心服务，智慧创造" image:nil location:nil urlResource:urlResource presentedController:self completion:^(UMSocialResponseEntity *shareResponse){
            if (shareResponse.responseCode == UMSResponseCodeSuccess) {
                NSLog(@"分享成功！");
                [self cancelAction:sender];
            }
        }];
    }
    else if ([self.shareType isEqualToString:@"coupons"])
    {
        [self getTimeNow];
        [ServiceManager putShareCouponsWithSequenceNBR:self.sequenceNBR success:^(NSDictionary *data) {
            
        } failure:^(NSDictionary *data) {
            
        }];
        [UMSocialData defaultData].extConfig.title = @"赠送优惠券";
        UMSocialUrlResource *urlResource = [[UMSocialUrlResource alloc] initWithSnsResourceType:UMSocialUrlResourceTypeImage url:
                                            @"http://o7s70rfog.bkt.clouddn.com/icon-umibus.png"];
        [UMSocialData defaultData].extConfig.qqData.url = [NSString stringWithFormat:@"%@/cuterats/sharelink?id=%@&random=%@",[ServiceManager getHostAddress],self.sequenceNBR,[self getTimeNow]];
        [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToQQ] content:@"优米巴士免费送流量话费了，领到就是赚到！" image:nil location:nil urlResource:urlResource presentedController:self completion:^(UMSocialResponseEntity *shareResponse){
            if (shareResponse.responseCode == UMSResponseCodeSuccess) {
                NSLog(@"分享成功！");
                [self cancelAction:sender];
            }
        }];
    }
}

- (IBAction)cancelAction:(id)sender
{
    [self dismissViewControllerAnimated:NO completion:^{
        
    }];
}
//获取当前系统时间 毫秒数
- (NSString *)getTimeNow
{
    NSTimeInterval time=[[NSDate date] timeIntervalSince1970]*1000;
    NSString *timeNow = [[NSString alloc] initWithFormat:@"%.f", time];
    NSLog(@"%@", timeNow);
    return timeNow;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
